/* 猴子选大王 */

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;


int main(void)
{
	int n[] = { 6, 12, 8 };
	int m[] = { 2, 4, 3 };
	int nCaseNum = sizeof(n) / sizeof(n[0]);

	for (int i = 0; i < nCaseNum; i++)
	{
		list<int> listMonkey(n[i], 0);
		int idx = 0;
		generate(listMonkey.begin(), listMonkey.end(), [&idx](void) -> int {return ++idx; });

		int size = listMonkey.size();
		auto iter = listMonkey.begin();
		while (size > 1)
		{
			int dist = (m[i] - 1) % size;
			for (int i = 0; i < dist; i++)
			{
				++iter;
				if (iter == listMonkey.end())
				{
					iter = listMonkey.begin();
				}
			}
			if (iter == listMonkey.end())
			{
				iter = listMonkey.begin();
			}
			iter = listMonkey.erase(iter);
			if (iter == listMonkey.end())
			{
				iter = listMonkey.begin();
			}

			size--;
		}

		cout << "The king is NO. " << *iter << endl;
	}
}
